package cn.umakr.web;

import cn.umakr.entity.Dept;
import cn.umakr.entity.UserInfo;
import cn.umakr.service.*;
import cn.umakr.util.PageUtil;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.PrinterGraphics;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangbo on 2017/7/20.
 */
@WebServlet(name = "ListServletAjax", urlPatterns = "/ListServletAjax")
public class ListServletAjax extends HttpServlet {

    private UserService userService = new UserService();
    private DeptService deptService = new DeptService();
    private UserInfoService userInfoService = new UserInfoService();
    private DeptPageService deptPageService = new DeptPageService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置request与response的编码
        response.setContentType("text/html");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String type = request.getParameter("type");
        String current = request.getParameter("currentpage");
        PrintWriter out = response.getWriter();
        if (type.equals("user")) {
            //查询用户列表数据
            List<UserInfo> userInfos = userInfoService.ListUserInfo();
            request.setAttribute("userInfos", userInfos);
            request.getRequestDispatcher("/pages/tab/tab-user.jsp").forward(request, response);
        } else if (type.equals("dept")) {
            //分页查询部门数据
            Integer page = Integer.valueOf(current);
            String ptype = request.getParameter("ptype");
            Map<String, Integer> pageMap = deptPageService.getPage(page, ptype);
            Integer currentPage = pageMap.get("page");
            Integer totalPage = pageMap.get("maxPage");
            Integer totalRecord = pageMap.get("maxCount");
            //查询部门列表数据
//            List<Dept> depts = deptService.findAllDepts();
            //分页查询部门数据
            List<Dept> depts = deptService.pageDepts(currentPage, PageUtil.getPageSize());
            Gson gson = new Gson();
            String deptsStr = gson.toJson(depts);
            Map<String, Object> deptPageMap = new LinkedHashMap<String, Object>();
            deptPageMap.put("deptsStr", depts);
            deptPageMap.put("currentPage", currentPage);
            deptPageMap.put("totalRecord", totalRecord);
            deptPageMap.put("totalPage", totalPage);
            String result = gson.toJson(deptPageMap);
            request.getSession().removeAttribute("currentPage");
            request.getSession().removeAttribute("totalRecord");
            request.getSession().removeAttribute("totalPage");
            request.getSession().removeAttribute("totalPage");
            request.getSession().removeAttribute("depts");
//            request.setAttribute("currentPage", currentPage);
//            request.setAttribute("totalRecord", totalRecord);
//            request.setAttribute("totalPage", totalPage);
//            request.setAttribute("depts", depts);
            out.print(result);
            out.flush();
            out.close();
//            request.getRequestDispatcher("/pages/tab/tab-dept.jsp").forward(request, response);
        }
    }

}
